<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Userinto_m extends FOX_Model
{
    function __construct()
    {
        parent::__construct();
    }

    public function add_userinto($data){
        $into = !empty($this->get_project($data['user_id'],$data['project_id']))?$this->get_project($data['user_id'],$data['project_id']):'';
        if($into){
            if($into['into_num'] != $data['into_num']){
                $this->update_userinto($data['user_id'],array('into_num'=>$data['into_num']));
            }else{
                return false;
            }

        }else{
            $this->db->insert('user_into',$data);
            if($this->db->affected_rows() > 0){
                return $this->db->insert_id();
            }else{
                return FALSE;
            }
        }
    }
    public function get_one_project_userinto($user_id){
        $this->db->select('*');
        $this->db->from('user_into');
        $this->db->where('user_id',$user_id);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->row_array();
        }else{
            return false;
        }

    }
    public function get_project($user_id,$project_id){
        $this->db->select('*');
        $this->db->from('user_into');
        $this->db->where('user_id',$user_id);
        $this->db->where('project_id',$project_id);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->row_array();
        }else{
            return false;
        }
    }
    public function update_userinto($user_id,$data){
        $this->db->where('user_id',$user_id);
        $this->db->update('user_into', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }
    public function count_userinto_find(){
        $this->db->select('into_id');
        $this->db->from('user_into');
        $this->db->where('delete',0);
        $total=$this->db->count_all_results();
        return $total;
    }
    public function get_userinto_list($limit,$start){
        $this->db->select('user_into.*,project.project_name,project.project_money,users.user_name,users.user_uname');
        $this->db->from('user_into');
        $this->db->join('project','project.project_id = user_into.project_id','left');
        $this->db->join('users','users.user_id = user_into.user_id','left');
        $this->db->limit($start,$limit);
        $this->db->where('user_into.delete',0);
        $this->db->order_by('user_into.into_id','desc');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    //查找某个用户的分成数
    public function get_userinto_user_list($limit,$start,$user_id){
        $this->db->select('user_into.*,project.project_name,project.project_money,users.user_name,users.user_uname');
        $this->db->from('user_into');
        $this->db->join('project','project.project_id = user_into.project_id','left');
        $this->db->join('users','users.user_id = user_into.user_id','left');
        $this->db->limit($start,$limit);
        $this->db->where('user_into.user_id',$user_id);
        $this->db->where('user_into.delete',0);
        $this->db->order_by('user_into.into_id','desc');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    public function get_one_userinto($into_id){
        $this->db->select('*');
        $this->db->from('user_into');
        $this->db->where('into_id',$into_id);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->row_array();
        }else{
            return false;
        }
    }

    public function update_userinto_by_into_id($into_id,$data){
        $this->db->where('into_id',$into_id);
        $this->db->update('user_into', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }
    public function update_userinto_by_intoid($into_id,$data){
        $this->db->where('into_id',$into_id);
        $this->db->update('user_into', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

        //查找某个用户的分成数
    public function get_userinto_money($user_id=''){
        $this->db->select('user_into.*,project.project_name,project.project_money,project.project_total_money,users.user_id,users.user_uname');
        $this->db->from('user_into');
        $this->db->join('project','project.project_id = user_into.project_id','left');
        $this->db->join('users','users.user_id = user_into.user_id','left');
        if($user_id){
            $this->db->where('user_into.user_id',$user_id);
        }
        $this->db->where('user_into.delete',0);
        $this->db->order_by('user_into.into_id','desc');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    public function update_userinto_by_project_id_id($project_id,$data){
        $this->db->where('project_id',$project_id);
        $this->db->update('user_into', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }
}
